package com.baoxin.chenxixc4coach;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.baoxin.chenxixc4coach.adapter.CourseStudentAdapter;
import com.baoxin.chenxixc4coach.bean.Course;
import com.baoxin.chenxixc4coach.bean.CourseStudent;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CourseDetailActivity extends BaseActivity {

    private Course course;
    private RecyclerView recyclerView;
    private CourseStudentAdapter adapter;
    private List<CourseStudent> courseStudentList;
    private Handler handler;

    public static void startAction(Context context, Course course) {
        Intent intent = new Intent(context, CourseDetailActivity.class);
        intent.putExtra("course", course);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        course = getIntent().getParcelableExtra("course");

        recyclerView = findViewById(R.id.recyclerview);
        courseStudentList = new ArrayList<>();
        adapter = new CourseStudentAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Msg.REQUEST_SUCCEED:
                        courseStudentList = (List<CourseStudent>) msg.obj;
                        adapter.setData(courseStudentList);
                        adapter.notifyDataSetChanged();
                        break;
                }
            }
        };

        request();
    }

    private void request() {
        JSONObject body = new JSONObject();
        try {
            body.put("ofadId", course.getOfadId());
            NetUtil.post(this, Interfaces.URL_COURSE_STUDENTS, body.toString(),
                    new MyCallback(Interfaces.URL_COURSE_STUDENTS, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 发送短信
     *
     * @param view
     */
    public void onSendAll(View view) {
        Intent intent = new Intent(this, SendMsgActivity.class);
        startActivity(intent);
    }
}
